import os
import re

def remove_lombok_from_file(file_path):
    """移除单个文件中的lombok注解"""
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
        
        # 检查是否包含lombok注解
        if not any(annotation in content for annotation in ['@Data', '@Getter', '@Setter', '@AllArgsConstructor', '@NoArgsConstructor', '@Builder', '@EqualsAndHashCode']):
            return False
        
        print(f"Processing: {file_path}")
        
        # 移除lombok导入
        content = re.sub(r'import lombok\.\*;\s*\n', '', content)
        content = re.sub(r'import lombok\.[^;]+;\s*\n', '', content)
        
        # 移除lombok注解
        content = re.sub(r'@Data\s*\n', '', content)
        content = re.sub(r'@Getter\s*\n', '', content)
        content = re.sub(r'@Setter\s*\n', '', content)
        content = re.sub(r'@AllArgsConstructor\s*\n', '', content)
        content = re.sub(r'@NoArgsConstructor\s*\n', '', content)
        content = re.sub(r'@Builder\s*\n', '', content)
        content = re.sub(r'@EqualsAndHashCode[^\n]*\n', '', content)
        
        # 对于枚举类，添加构造函数和getter方法
        if 'enum ' in content and '@Getter' in content:
            # 这是枚举类，需要特殊处理
            process_enum_file(file_path, content)
        else:
            # 写回文件
            with open(file_path, 'w', encoding='utf-8') as f:
                f.write(content)
        
        return True
    except Exception as e:
        print(f"Error processing {file_path}: {e}")
        return False

def process_enum_file(file_path, content):
    """处理枚举文件"""
    # 简单移除注解即可，枚举不需要复杂的方法生成
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(content)

def process_directory(directory):
    """处理目录下的所有Java文件"""
    processed_count = 0
    
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.java'):
                file_path = os.path.join(root, file)
                if remove_lombok_from_file(file_path):
                    processed_count += 1
    
    return processed_count

if __name__ == '__main__':
    # 处理enums目录
    enums_dir = r'd:\projects\zhixin\minimalist-saas\minimalist-common\src\main\java\com\minimalist\common\entity\lindi\enums'
    po_dir = r'd:\projects\zhixin\minimalist-saas\minimalist-common\src\main\java\com\minimalist\common\entity\lindi\po'
    
    print("开始处理enums目录...")
    enums_count = process_directory(enums_dir)
    print(f"enums目录处理完成，共处理 {enums_count} 个文件")
    
    print("\n开始处理po目录...")
    po_count = process_directory(po_dir)
    print(f"po目录处理完成，共处理 {po_count} 个文件")
    
    print(f"\n总计处理 {enums_count + po_count} 个文件")